using MicroMsg.Common.Audio.Amr;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class AmrPlayer
	{
		private static string TAG = "AmrPlayer";
		private Thread soundThread;
		private Stream mAmrStream;
		private float mvolume = 1f;
		private MediaElementState cuState;
		private AutoResetEvent autoResetEvent = new AutoResetEvent(false);
		private VoicePlayStatus playerStatus;
		private bool isForceStop;
		private bool isShouldDestroy;
		public List<MediaElement> UiObjectList = new List<MediaElement>();
		public event RoutedEventHandler MediaOpened;
		public event EventHandler<ExceptionRoutedEventArgs> MediaFailed;
		public event RoutedEventHandler MediaEnded;
		public object Tag
		{
			get;
			set;
		}
		public float Volume
		{
			get
			{
				return this.mvolume;
			}
			set
			{
				this.mvolume = value;
			}
		}
		public MediaElementState CurrentState
		{
			get
			{
				return this.cuState;
			}
		}
		public void Play()
		{
			this.Stop();
			if (this.soundThread == null)
			{
				this.soundThread = new Thread(new ThreadStart(this.PlayThreadProc));
				this.soundThread.Start();
			}
			if (this.playerStatus == VoicePlayStatus.Idle)
			{
				Log.smethod_5(AmrPlayer.TAG, "playerStatus = VoicePlayStatus.Playing");
				this.playerStatus = VoicePlayStatus.Playing;
				this.autoResetEvent.Set();
			}
		}
		public void Stop()
		{
			if (this.playerStatus != VoicePlayStatus.Idle)
			{
				this.isForceStop = true;
				while (this.playerStatus == VoicePlayStatus.Playing)
				{
					Thread.Sleep(10);
				}
			}
		}
		public void SetSource(Stream amrStream)
		{
			if (this.playerStatus == VoicePlayStatus.Playing)
			{
				Log.smethod_5(AmrPlayer.TAG, "SetSource error,playerStatus == VoicePlayStatus.Playing");
				return;
			}
			if (amrStream == null)
			{
				Log.smethod_5(AmrPlayer.TAG, "SetSource error,amrStream == null");
				return;
			}
			int num = 4096;
			byte[] array = new byte[4096];
			this.mAmrStream = new MemoryStream();
			int num2;
			while ((num2 = amrStream.Read(array, 0, num)) > 0)
			{
				this.mAmrStream.Write(array, 0, num2);
			}
		}
		public void ClearValue()
		{
		}
		private void PlayThreadProc()
		{
			while (true)
			{
				this.autoResetEvent.WaitOne();
				Log.smethod_5(AmrPlayer.TAG, "PlayThreadProc get autoResetEvent,thread start.");
				MediaElement uIElement = this.getUIElement();
				if (uIElement != null)
				{
					Log.smethod_5(AmrPlayer.TAG, "in thread:MediaOpened ");
					uIElement.get_Dispatcher().BeginInvoke(delegate
					{
						if (this.MediaOpened != null)
						{
							this.MediaOpened.Invoke(null, null);
						}
					});
				}
				short[] array = new short[160];
				byte[] array2 = new byte[32];
				short[] array3 = new short[]
				{
					12,
					13,
					15,
					17,
					19,
					20,
					26,
					31,
					5,
					0,
					0,
					0,
					0,
					0,
					0,
					0
				};
				AMRDecodeFloat aMRDecodeFloat = new AMRDecodeFloat();
				List<MemoryStream> list = new List<MemoryStream>();
				SoundEffect soundEffect = null;
				SoundEffectInstance soundEffectInstance = null;
				int num = 3;
				int num2 = 14250;
				MemoryStream memoryStream = null;
				long num3 = 0L;
				long num4 = 15L;
				if (this.mAmrStream != null)
				{
					this.mAmrStream.Seek(0L, 0);
				}
				while (this.mAmrStream != null && !this.isForceStop)
				{
					try
					{
						FrameworkDispatcher.Update();
						goto IL_305;
					}
					catch
					{
						goto IL_305;
					}
					goto IL_BA;
					IL_153:
					int num5 = 0;
					int num6;
					while ((num6 = this.mAmrStream.Read(array2, 0, 1)) > 0)
					{
						int num7 = array2[0] >> 3 & 15;
						int num8 = (int)array3[num7];
						this.mAmrStream.Read(array2, 1, num8);
						int num9 = 0;
						if (aMRDecodeFloat.amr2pcm(array2, num8 + 1, array, ref num9, 1024))
						{
							num3 += 1L;
							if (num3 > num4)
							{
								byte[] array4 = new byte[array.Length * 2];
								Buffer.BlockCopy(array, 0, array4, 0, array4.Length);
								if (memoryStream == null)
								{
									memoryStream = new MemoryStream();
								}
								memoryStream.Write(array4, 0, 2 * num9);
								num5++;
								if (memoryStream.get_Length() >= (long)num2)
								{
									list.Add(memoryStream);
									memoryStream = null;
								}
								if (num5 >= num)
								{
									break;
								}
							}
						}
					}
					if (num6 > 0)
					{
						continue;
					}
					if (memoryStream != null)
					{
						list.Add(memoryStream);
						memoryStream = null;
						continue;
					}
					if (list.get_Count() > 0)
					{
						continue;
					}
					if (soundEffect == null)
					{
						break;
					}
					if (soundEffectInstance.get_State() == null)
					{
						continue;
					}
					soundEffectInstance.Stop();
					soundEffectInstance = null;
					soundEffect = null;
					break;
					IL_305:
					Thread.Sleep(33);
					if (list.get_Count() == 0)
					{
						goto IL_153;
					}
					IL_BA:
					if (soundEffectInstance == null)
					{
						MemoryStream memoryStream2 = list.get_Item(0);
						list.RemoveAt(0);
						soundEffect = new SoundEffect(memoryStream2.ToArray(), 8000, 1);
						soundEffectInstance = soundEffect.CreateInstance();
						soundEffectInstance.set_Volume(this.Volume);
						soundEffectInstance.Play();
						goto IL_153;
					}
					if (soundEffectInstance.get_State() != null)
					{
						soundEffectInstance.Stop();
						soundEffectInstance = null;
						soundEffect = null;
						MemoryStream memoryStream3 = list.get_Item(0);
						list.RemoveAt(0);
						soundEffect = new SoundEffect(memoryStream3.ToArray(), 8000, 1);
						soundEffectInstance = soundEffect.CreateInstance();
						soundEffectInstance.set_Volume(this.Volume);
						soundEffectInstance.Play();
						goto IL_153;
					}
					goto IL_153;
				}
				if (soundEffectInstance != null)
				{
					soundEffectInstance.Stop();
					soundEffectInstance = null;
					soundEffect = null;
				}
				memoryStream = null;
				list.Clear();
				list = null;
				if (!this.isForceStop && uIElement != null)
				{
					Log.smethod_5(AmrPlayer.TAG, "in thread:MediaEnded ");
					uIElement.get_Dispatcher().BeginInvoke(delegate
					{
						if (this.MediaEnded != null)
						{
							this.MediaEnded.Invoke(null, null);
						}
					});
				}
				this.isForceStop = false;
				this.playerStatus = VoicePlayStatus.Idle;
				Log.smethod_5(AmrPlayer.TAG, "playerStatus = VoicePlayStatus.Idle");
			}
		}
		public MediaElement getUIElement()
		{
			if (this.UiObjectList.get_Count() <= 0)
			{
				return null;
			}
			return this.UiObjectList.get_Item(0);
		}
	}
}
